package com.zlx.controller;

import com.alibaba.fastjson.JSONObject;
import com.zlx.constants.Constant;
import com.zlx.dao.WeChatService;
import com.zlx.entities.User;
import com.zlx.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Author 周罗校
 * @Description 微信登陆
 * @ClassName WeChatServlet
 * @date 2020.09.27 17:07:02
 * 该版权归周罗校所有，最终解释权以官方为准
 */
@WebServlet("/WeChat/*")
public class WeChatServlet extends BaseServlet {
    private WeChatService weChatService = new WeChatService();

    protected void towx_login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String appid = "wx3500b25b23938d73";
        String redirectUri = "http://ktc.dfbz.com/WeChat/wx_login";
        String appSecret = "56ee69a0faf098b3ce3c808cc6dac1ff";
        String url = "https://open.weixin.qq.com/connect/qrconnect" +
                "?appid=" + appid +
                "&redirect_uri=" + redirectUri +
                "&response_type=code" +
                "&scope=snsapi_login";
        response.sendRedirect(url);
    }
    /**
     * @Author 周罗校
     * @Description 微信登陆回调
     * @Param [request, response]
     * @return void
     * @Date 2020.09.27 19:41:09
     **/
    protected void wx_login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();


        String code = request.getParameter("code");


        // https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

        String appid = "wx3500b25b23938d73";
        String AppSecret = "56ee69a0faf098b3ce3c808cc6dac1ff";

        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid +
                "&secret=" + AppSecret +
                "&code=" + code +
                "&grant_type=authorization_code";

        // 获取AccessToken、openid等数据
        JSONObject authInfo = HttpUtils.auth(url);
//        System.out.println("authInfo: " + authInfo);
        url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + authInfo.getString("access_token") +
                "&openid=" + authInfo.getString("openid");
        //转换微信登陆的信息为Json字符串
        JSONObject userInfo = HttpUtils.getUserInfo(url);
//        System.out.println("userInfo: " + userInfo);

        String openid = userInfo.getString("openid");
        User user = weChatService.queryUserByUserName(openid);
        if (user == null) {//添加用户
            user = new User();
            String sex = userInfo.getString("sex");
            String nickname = userInfo.getString("nickname");
            String headimgurl = userInfo.getString("headimgurl");
            user.setWxOpenid(openid);
            user.setGender(sex);
            user.setUsername(nickname);
            user.setPic(headimgurl);
            weChatService.insertWeChatUser(user);
            user = weChatService.queryUserByUserName(openid);
        }
        session.setAttribute(Constant.LOGIN_USER, user);
        response.sendRedirect("/user/list");
    }

}
